package com.yksj.healthtalk.ui.chatting;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.chatting.InviteAttentionActivity.FriendsListAdapter;
import com.yksj.healthtalk.ui.chatting.InviteAttentionActivity.ViewHolder;
import com.yksj.healthtalk.utils.DataParseUtil;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 邀请好友搜索结果列表
 * @author jack_tang
 *
 */
public class InviteFriendsListActivity extends BaseFragmentActivity implements OnClickListener{
	ListView mListView;
	FriendsListAdapter mAdapter;
	String groupId;
	String name;
	private int pageSize = 1;
	private int pageNum = 20;
	private PullToRefreshListView mPullToRefreshListView; 
	
	final ObjectHttpResponseHandler mHandler = new ObjectHttpResponseHandler(this) {
		
		@Override
		public Object onParseResponse(String cotent) {
			return DataParseUtil.parseInviteResultData(cotent);
		}
		public void onSuccess(int statusCode, Object response) {
			if(response instanceof String){
				//邀请错误
				ToastUtil.showShort(InviteFriendsListActivity.this,String.valueOf(response));
			}else{
				mAdapter.onDataChange((List<CustomerInfoEntity>)response);
			}
		};
		public void onFinish() {
			super.onFinish();
			mPullToRefreshListView.onRefreshComplete();
		};
	};
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.invite_friend_by_name_layout);
		groupId = getIntent().getStringExtra("groupId");
		name = getIntent().getStringExtra("parame1");
		
		initView();
		
		onQueryData(pageSize, 20);
	}
	
	private void initView(){
		initTitle();
		
		setTitle(name);
		
		setBackBtn("返回",this);
		setRight("确定",this);
		
		mPullToRefreshListView = (PullToRefreshListView)findViewById(R.id.listview);
		mPullToRefreshListView.setRefreshing();
		mListView = mPullToRefreshListView.getRefreshableView();
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ViewHolder holder = (ViewHolder)view.getTag();
				boolean  isChecked = holder.chooseBox.isChecked();
				CustomerInfoEntity entity = mAdapter.getItem(position-1);
				String userid = entity.getId();
				mAdapter.onChoose(userid,!isChecked);
				holder.chooseBox.setChecked(!isChecked);
			}
		});
		mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				onQueryData(++pageSize,pageNum);
			}
		});
		mAdapter = new FriendsListAdapter(getLayoutInflater(),this);
		mListView.setAdapter(mAdapter);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2://邀请
			onSubmitInviteFriends();
			break;	
		}
	}
	
	/**
	 * 按名字查询
	 */
	private void onQueryData(int pageSize,int  pageNum){
		HttpRestClient.doHttpInviteByName(SmartFoxClient.getLoginUserId(), name, pageSize, pageNum, mHandler);
	}
	
	private void onSubmitInviteFriends(){
		List<String> list = new ArrayList<String>(mAdapter.mChoosedMap.keySet());
		if(list != null && list.size()>0){
			SmartFoxClient.sendGroupInvite(this,groupId,list);
			ToastUtil.showShort(getApplicationContext(),"邀请已发出");
		}
	}
}
